package com.example.musicservice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private ServiceConnection mConnection;
    private MusicService.MyBinder mMusicControl;
    private SeekBar mSeekBar;
    private Handler mHandler;
    private Runnable mRunnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mHandler = new Handler();
        mRunnable = new Runnable() {//用来定时执行的
            @Override
            public void run() {
                mSeekBar.setProgress(mMusicControl.getCurrenPostion());
                mHandler.postDelayed(this, 200);
            }
        };
        //mHandler.postDelayed(mRunnable, 200);//200ms后开始执行
        //mHandler.removeCallbacks(mRunnable);//停止计时器

        mSeekBar = findViewById(R.id.seek_bar);
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {//进度条改变
                if (fromUser && mMusicControl.isPlaying()){//这里有问题，暂停后拖动
                    mMusicControl.seekTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {//开始触摸

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {//停止触摸

            }
        });



        Intent intent = new Intent(this, MusicService.class);
        startService(intent);//混合方式打开Service
        mConnection = new ServiceConnection() {//服务启动完成后会进入到这个方法
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                //获得service中的MyBinder
                mMusicControl = (MusicService.MyBinder) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        bindService(intent, mConnection , BIND_AUTO_CREATE);


        findViewById(R.id.btn_play).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMusicControl != null) {
                    mMusicControl.play("/mnt/sdcard/Music/林俊杰 - 起风了（Live）.flac");
                    //设置进度条的最大值
                    mSeekBar.setMax(mMusicControl.getDuration());
                    //设置进度条的进度
                    mSeekBar.setProgress(mMusicControl.getCurrenPostion());
                    mHandler.postDelayed(mRunnable, 200);
                    Log.d("musicservice", "clickplay");
                    //mHandler.postDelayed(mRunnable, 200);
                } else {
                    Log.d("musicservice", "clickerror");
                }
            }
        });

        findViewById(R.id.btn_pause).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMusicControl.pause();
                mHandler.removeCallbacks(mRunnable);
            }
        });

        findViewById(R.id.btn_resume).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMusicControl.resume();
                mHandler.postDelayed(mRunnable, 200);
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //退出应用后与service解除绑定
        unbindService(mConnection);
    }

}